# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=babl
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.1.114
pkgrel=1
pkgdesc="Dynamic Pixel Format Translation Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gegl.org/babl/"
msys2_repository_url="https://gitlab.gnome.org/GNOME/babl"
license=("spdx:LGPL-3.0-or-later")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-vala"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-lcms2")
source=("https://download.gimp.org/pub/babl/${pkgver%.*}/${_realname}-${pkgver}.tar.xz"
        "001-file-open-unicode.patch"
        "002-disable-codeview.patch")
sha256sums=('bcbb7786c1e447703db3bc7fa34d62d0d2d117b22f04d8834c7b2d5ded456487'
            '98cf43394eb1d1a8140438fbb745ed31f3768c07086afab297e4be9d609d07c5'
            '8f7a06277921486ca3bbab6e0d4f459b23cbbf507fa4e864fcc867da06c9bbe0')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"
  apply_patch_with_msg \
    001-file-open-unicode.patch \
    002-disable-codeview.patch
}

build() {
  mkdir -p "build-${MSYSTEM}" && cd "build-${MSYSTEM}"

  if [ "${CARCH}" = "i686" ]; then
    _arch_opt="-Denable-sse2=false"
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson setup \
    --prefix="${MINGW_PREFIX}" \
    --buildtype=plain \
    -Dwith-lcms=true \
    -Denable-gir=true \
    -Dwith-docs=false \
    ${_arch_opt} \
    "../${_realname}-${pkgver}"

  meson compile
}

package() {
  cd "build-${MSYSTEM}"

  meson install --destdir "${pkgdir}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
